<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends GuestController
{
	/**
	* Constructeur
	*/
	public function __construct()
	{
		parent::__construct();
		
		//On change le titre de la page
		parent::setPageName('Index');
		
		//On change la vue du menu
		if($this->session->userdata('is_connected') != 1)
		{
			parent::setMenuView('menu/home_menu');
		}
		else
		{
			parent::setMenuView('menu/home_menu_co');
		}
	}
	
	public function deconnexion()
	{
		$this->session->set_userdata('is_connected',0);
		$this->session->set_userdata('user_obj',NULL);
		
		$this->session->set_userdata('notif_ok','<div class="alert alert-success"><button type="button" class="close" data-dismiss="alert">×</button><strong>Deconnexion réussite !</strong></div>');
		redirect('home/index', 'refresh');
	}
	
	public function login_validation()
	{
		$this->load->library('form_validation');
		$this->load->helper('form');
		$this->form_validation->set_error_delimiters('<div class="alert alert-error"><button type="button" class="close" data-dismiss="alert">×</button>', '</div>');
			
		//mise en place des regles
		$this->form_validation->set_rules('login_user', 'Email', 'required|valid_email');
		$this->form_validation->set_rules('pass_user', 'Mot de passe', 'required');
		$password_ok = false;
		
		if ($this->form_validation->run() != FALSE) // reussite
		{

			$user = User::getUserByEmail($this->input->post('login_user'));
			if($user == NULL)
			{
				$this->session->set_userdata('notif_err','<div class="alert alert-error"><button type="button" class="close" data-dismiss="alert">×</button>L\'utilisateur n\'existe pas !<div/>');
				$this->login();
			}
			else
			{
				$user = (array) $user;
				
				$this->session->set_userdata('is_connected',1);
				$this->session->set_userdata('user_obj',$user);
				$this->session->set_userdata('notif_ok','<div class="alert alert-success"><button type="button" class="close" data-dismiss="alert">×</button><strong>Connexion réussite !</strong> Bienvenue '.$user['nom'] .' '.$user['prenom'].'.</div>');
				redirect('home/index', 'refresh');
			}
		}
		else
		{
			$this->login();
			
		}
	}
	
	public function login()
	{
		parent::setPageName('Connexion');
		parent::setMenuView('menu/login_menu');
		parent::loadHeader();
		
		$this->load->view('notification_zone');
		$this->load->view('home/login');
		
		parent::loadFooter();
	}
	
	/**
	 * Page d'accueil
	 */
	public function index()
	{
		parent::loadHeader();
		
		$this->load->view('notification_zone');
		$this->load->view('home/index');
		
		parent::loadFooter();
	}

	public function view_event()
	{
		parent::loadHeader();
		
		$this->load->view('event/viewEvent');
		
		parent::loadFooter();
    }

    public function view_lieu()
    {
        $this->view_event();
    }

}
